/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.swing.swixml.adapter;

import java.awt.LayoutManager;

import nemos.swing.ILayoutFactory;

import org.swixml.LayoutConverter;
import org.swixml.dom.Attribute;
import org.w3c.dom.Element;

/**
 * LayoutConverterAdapter : The SwiXML adapter for OSGi layout factories.
 * 
 * <p>
 * This class adapts {@link ILayoutFactory} service into a SwiXML
 * {@link LayoutConverter}.
 * </p>
 * 
 * @author Romain DUBOIS
 */
public class LayoutConverterAdapter implements LayoutConverter {

    /** The layout ID */
    private final String mLayoutId;

    /** The adapted layout converter */
    private final ILayoutFactory mAdapted;

    /**
     * Constructor.
     * 
     * @param pLayoutId
     *            The layout ID
     * @param pAdapted
     *            The layout factory to adapt
     */
    public LayoutConverterAdapter(final String pLayoutId, final ILayoutFactory pAdapted) {
        super();
        mLayoutId = pLayoutId;
        mAdapted = pAdapted;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.LayoutConverter#getID()
     */
    @Override
    public String getID() {
        return mLayoutId;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.LayoutConverter#convertLayoutAttribute(org.swixml.dom.Attribute)
     */
    @Override
    public LayoutManager convertLayoutAttribute(final Attribute attr) {
        return mAdapted.createLayout(attr.getValue());
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.LayoutConverter#convertLayoutElement(org.w3c.dom.Element)
     */
    @Override
    public LayoutManager convertLayoutElement(@SuppressWarnings("unused") final Element element) {
        throw new UnsupportedOperationException();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.LayoutConverter#convertConstraintsAttribute(org.swixml.dom.Attribute)
     */
    @Override
    public Object convertConstraintsAttribute(final Attribute attr) {
        return mAdapted.getConstraints(attr.getValue());
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.LayoutConverter#convertConstraintsElement(org.w3c.dom.Element)
     */
    @Override
    public Object convertConstraintsElement(@SuppressWarnings("unused") final Element element) {
        throw new UnsupportedOperationException();
    }
}
